<?php
class Card {

  var $id;
  var $name;
  var $ref;
  var $cost;
  var $volition;
  var $offense;
  var $defense;
  var $life;
  var $isUnique;
  var $isEncounter;
  var $type;
  var $sphere;
  var $pack;
      
  function Card() {
  }


  static function getCards($deckId) {
    $query = "SELECT c.Id, c.Name, c.Ref, c.Volition, c.Offense, c.Defense, c.Life, c.IsEncounter, c.IsUnique,
                     ct.Id AS TypeId, ct.Name AS TypeName, ct.ImageName AS TypeImageName
                     s.Id AS SphereId, s.Name AS SphereName, s.ImageName AS SphereImageName,
                     p.Id AS PackId, p.Name AS PackName  
              FROM DeckCard AS dc
              JOIN Card AS c ON c.Id=dc.CardId
              JOIN CardType AS ct ON ct.Id = c.TypeId
              JOIN Sphere AS s ON s.Id = c.SphereId
              JOIN Pack AS p ON p.Id = c.PackId
              WHERE dc.DeckId=".$deckId;
    $result = mysql_query($query);
    return cast($result);  
  }


  static function getCards($constraint) {
    $query = "SELECT c.Id, c.Name, c.Ref, c.Volition, c.Offense, c.Defense, c.Life, c.IsEncounter, c.IsUnique,
                     ct.Id AS TypeId, ct.Name AS TypeName, ct.ImageName AS TypeImageName
                     s.Id AS SphereId, s.Name AS SphereName, s.ImageName AS SphereImageName,
                     p.Id AS PackId, p.Name AS PackName  
              FROM Card AS c 
              JOIN CardType AS ct ON ct.Id = c.TypeId
              JOIN Sphere AS s ON s.Id = c.SphereId
              JOIN Pack AS p ON p.Id = c.PackId
              WHERE ".$constraint;
    $result = mysql_query($query);
    return cast($result);
    
  }
  
  static function cast($cardsResult) {
    $cards = array();
    $index = 0;
    
    while($row = mysql_fetch_object($cardsResult)) {
      $type = new Type();
      $type->id = $row->TypeId;
      $type->name = $row->TypeName;
      $type->imageName = $row->TypeImageName;
      
      $sphere = new Sphere();
      $sphere->id = $row->SphereId;
      $sphere->name = $row->SphereName;
      $sphere->imageName = $row->SphereImageName;
      
      $pack = new Pack();
      $pack->id = $row->PackId;
      $pack->name = $row->PackName;
      
      $card = new Card();
      $card->id = $row->Id;
      $card->name = $row->Name;
      $card->ref = $row->Ref;
      $card->volition = $row->Volition;
      $card->offense = $row->Offense;
      $card->defense = $row->Defense;
      $card->life = $row->Life;
      $card->isEncounter = $row->IsEncounter;
      $card->isUnique = $row->IsUnique;
      $card->type = $type;
      $card->sphere = $sphere;
      $card->pack = $pack;
            
      $cards[$index] = $card;
      $index++;
    }
    return $cards;
  }


}




?>
